package ru.myrtle.umf.tools.ugen.templates;

import java.io.*;
import java.text.*;
import java.util.*;
import ru.myrtle.umf.*;
import ru.myrtle.umf.codegen.*;
import ru.myrtle.umf.codegen.java.*;
import ru.myrtle.umf.tools.ugen.*;

public class UEnumerationTemplate extends UTemplate
{
    
    private String comma(boolean yes) {
        return yes ? "," : ";";
    }
	
    protected final String NL = System.getProperties().getProperty("line.separator");
    protected final String TEXT_1 = "package ";
    protected final String TEXT_2 = ";" + NL + NL;
    protected final String TEXT_3 = NL + "public enum ";
    protected final String TEXT_4 = " {" + NL + NL;
    protected final String TEXT_5 = NL;
    protected final String TEXT_6 = "    ";
    protected final String TEXT_7 = "(" + NL + "        new ";
    protected final String TEXT_8 = "()" + NL + "    )";
    protected final String TEXT_9 = NL + NL;
    protected final String TEXT_10 = "    ";
    protected final String TEXT_11 = NL + NL;
    protected final String TEXT_12 = "    private final ";
    protected final String TEXT_13 = " delegate;" + NL + NL + "    ";
    protected final String TEXT_14 = "(";
    protected final String TEXT_15 = " delegate) {" + NL + "        this.delegate = delegate;" + NL + "    }" + NL;
    protected final String TEXT_16 = NL + NL + NL + "}" + NL;
    protected final String TEXT_17 = NL;

    /*
     *
     */


    public UEnumerationTemplate() {

    }

    /*
     *
     */

    public String generate(UTemplateContext context) throws UTemplateRejectException
    {
        final StringBuffer stringBuffer = new StringBuffer();

    checkTemplateObjectType(context, UEnumeration.class);

    UEnumeration object = (UEnumeration) context.getObject();

    String enumClassName     = UGenUtil.getEnumerationClassName(object);
    String enumClassPackage  = UGenUtil.getEnumerationClassPackage(object);

    String enumDelegateClassName     = UGenUtil.getEnumerationDelegateClassName(object);
    String enumDelegateClassPackage  = UGenUtil.getEnumerationDelegateClassPackage(object);

    context.getResource().setObjectFileName(enumClassName);
    context.getResource().setObjectFileExtension("java");
    context.getResource().setObjectPackage(enumClassPackage);

    boolean hasOperations = !object.getOperations().isEmpty();

    JavaImportBuilder importBuilder = new JavaImportBuilder();

    createDoc();
    addDocDescription(
            object.getDescription()
    );
    addDocAnnotation("generated");

        stringBuffer.append(TEXT_1);
        stringBuffer.append(enumClassPackage);
        stringBuffer.append(TEXT_2);
        stringBuffer.append(getDocString());
        stringBuffer.append(TEXT_3);
        stringBuffer.append(enumClassName);
        stringBuffer.append(TEXT_4);

    for (Iterator<UEnumerationLiteral> it = object.getLiterals().iterator(); it.hasNext();) {
        UEnumerationLiteral literal = it.next();

        String enumDelegateImplClassName     = UGenUtil.getEnumerationDelegateImplClassName(literal);
        String enumDelegateImplClassPackage  = UGenUtil.getEnumerationDelegateImplClassPackage(literal);

        if (hasOperations) {
            importBuilder.addPackage(enumDelegateImplClassPackage);
        }

        createDoc("    ");
        addDocDescription(
            literal.getDescription()
        );
        addDocAnnotation("generated");

        stringBuffer.append(getDocString());
        stringBuffer.append(TEXT_5);

        if (hasOperations) {

        stringBuffer.append(TEXT_6);
        stringBuffer.append(literal.getName());
        stringBuffer.append(TEXT_7);
        stringBuffer.append(enumDelegateImplClassName);
        stringBuffer.append(TEXT_8);
        stringBuffer.append(comma(it.hasNext()));
        stringBuffer.append(TEXT_9);

        }
        else {

        stringBuffer.append(TEXT_10);
        stringBuffer.append(literal.getName());
        stringBuffer.append(comma(it.hasNext()));
        stringBuffer.append(TEXT_11);

        }
    }

    if (hasOperations) {

        stringBuffer.append(TEXT_12);
        stringBuffer.append(enumDelegateClassName);
        stringBuffer.append(TEXT_13);
        stringBuffer.append(enumClassName);
        stringBuffer.append(TEXT_14);
        stringBuffer.append(enumDelegateClassName);
        stringBuffer.append(TEXT_15);

    }

        stringBuffer.append(TEXT_16);

    insertImport(stringBuffer, importBuilder, enumClassPackage);

        stringBuffer.append(TEXT_17);

        return stringBuffer.toString();
    }

    // ---------------------------------------------------------------------------------------
    // ---------------------------------------------------------------------------------------

}
